﻿/*******************************************************************************
* Copyright (C) Gasgoo Corporation. All rights reserved.
* 
* Author: Zhu.Xing.Chen
* Create Date: 2012-03-06
* Description: jquery.layer.js
*          
* Revision History:
* Date         Author               Description
* 
*********************************************************************************/
(function($) {
    $.layer = {
        /*
        * _inIt 函数入口点, 用下划线标示为私有方法外面程序不能直接调用。
        */
        _inIt: function(settings) {
            settings.jqObj.click(function() {
                $.layer._construct(settings);
                settings.useFx ? $.layer.doEffects(settings, "open") : null;
                settings.isDrag ? $.layer.doresize(settings) : null;
            });
        },
        //构造弹出层
        _construct: function(settings) {
            var jqPopupBlk = $("#" + settings.popupBlk),
                width = settings.auto ? jqPopupBlk.width() + 16 <= settings.maxWidth ? jqPopupBlk.width() + 16 : settings.maxWidth : settings.width,
                height = settings.auto ? jqPopupBlk.height() <= settings.maxHeight ? jqPopupBlk.height() : settings.maxHeight : settings.height,
                offset = settings.jqObj.offset(),
                popupLayer = $(document.createElement("div")).addClass(settings.popupLayerClass)
                            .css({ "top": offset.top, "left": offset.left, width: width + 22, height: height + 42 }),               //初始化最外层容器
                popupTitle = $(document.createElement("div")).addClass("js-popup-title").css("width", width + "px"), //初始化标题
                popupClose = $(document.createElement("span")),
                popupContent = $(document.createElement("div")).css({ "overflow": "auto", width: width, height: height, "margin-left": "10px", "background": "#fff" }).append($(document.createElement("div")).css({ width: jqPopupBlk.width(), height: jqPopupBlk.height() }).append(jqPopupBlk.html())), //初始内容
                popupIframe = $(document.createElement("iframe")).attr({ border: 0, frameborder: 0 }).css({position:"absolute",'z-index':-1,left:0,top:0,opacity:0,width:width,height:height});  //容器遮罩,用于屏蔽ie6下的select
            $("body").append(popupLayer.append(popupTitle.append(settings.title).append(popupClose)).append(popupContent).append(popupIframe));
            this._loadOverlay();
            popupClose.click(function() {
                $.layer.close(settings);
            });
        },
        //加载遮罩
        _loadOverlay: function() {
            this.overlay ? this.overlay.remove() : null;
            this.overlay = $(document.createElement("div")).append($(document.createElement("iframe")).attr({ border: 0, frameborder: 0 }).css({position:"absolute",'z-index':-1,left:0,top:0,opacity:0,width:"100%",height:"100%"}));
            this.overlay.addClass("js-overlay");
            $("body").append(this.overlay); //子级
            //$(parent.document.getElementById("js-overlay")).show(); //父级
        },
        //拖动
        doresize: function(settings) {
            var top, left, move = false, jqPopupLayer = $("." + settings.popupLayerClass); ;
            $(".js-popup-title").mousedown(function(e) {
                top = e.pageY - parseInt(jqPopupLayer.css("top"));
                left = e.pageX - parseInt(jqPopupLayer.css("left"));
                move = true;
            });
            $(document).mousemove(function(e) {
                if (move) {
                    var x = e.pageX - left;
                    var y = e.pageY - top;
                    jqPopupLayer.css({ "left": x, "top": y });
                }
            }).mouseup(function() {
                move = false;
            });
        },
        //改变容器位置
        setPosition: function() { },
        //特效
        doEffects: function(settings, way) {
            var jqPopupLayer = $("." + settings.popupLayerClass);
            if (way == "open") {
                jqPopupLayer.css({ "left": ($(document).width() - jqPopupLayer.width()) / 2, "top": (document.documentElement.clientHeight - jqPopupLayer.height()) / 2 }).show();
            } else {
                //$(parent.document.getElementById("js-overlay")).hide();
                $.layer.overlay.remove();
                jqPopupLayer.remove();
            }
        },
        //关闭
        close: function(settings) {
            this.doEffects(settings, "close");
        }
    };
    $.fn.layer = function(options) {
        /*
        *  初速化信息
        *  背景、拖动、宽度、高度、标题、关闭事件、容器位置、触发事件类型、特效、自适应
        */
        var settings = {
            isBack: true,
            isDrag: true,
            popupBlk: "",
            popupLayerClass: "js-popup-layer",
            width: 760,
            height: 460,
            maxWidth: 760,
            maxHeight: 460,
            title: "",
            close: "",
            offsets: {
                x: 0,
                y: 0
            },
            eventType: "click",
            useFx: true,
            auto: false,
            jqObj: $(this)
        };
        $.extend(true, settings, options || {});
        $.layer._inIt(settings);
    }
})(jQuery);